package com.mo.tools.qrcode;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: e-Xingfu.Yang
 * @description:
 * @date:created in 2021/9/22 11:33
 * @modificed by:
 */
public class ZxingUtils {
    private static final Logger log = LoggerFactory.getLogger(ZxingUtils.class);

    private static int WIDTH = 300;
    private static int HEIGHT = 300;
    private static final String SUFFIX_NAME = "png";

    public static boolean createQrcode(String content, String savePath, String logoPath) throws WriterException, IOException {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(3);
        hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.toString());
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
        hints.put(EncodeHintType.MARGIN, 2);
        BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < bitMatrix.getWidth(); x++) {
            for (int y = 0; y < bitMatrix.getHeight(); y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
            }
        }
        graphics2D.drawImage(image, 0, 0, null);

        if (!ObjectUtils.isEmpty(logoPath)) {
            File file = new File(logoPath);
            if (!file.exists()) {
                log.info("图片不存在");
                return false;
            } else {
                BufferedImage logo = ImageIO.read(file);

                int width_4 = WIDTH / 4;
                int width_8 = width_4 / 2;
                int height_4 = HEIGHT / 4;
                int height_8 = height_4 / 2;
                graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
                graphics2D.drawImage(logo, width_4 + width_8, height_4 + height_8, width_4, height_4, null);
            }
        }
        graphics2D.dispose();
        bufferedImage.flush();
        ImageIO.write(bufferedImage, SUFFIX_NAME, new File(savePath));
        return true;
    }


    public static Result readQrcode(String filePath) throws NotFoundException, IOException {
        MultiFormatReader reader = new MultiFormatReader();
        File file = new File(filePath);
        BufferedImage image = ImageIO.read(file);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(1);
        hints.put(DecodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.toString());
        return reader.decode(binaryBitmap, hints);
    }


}
